/**
 * 
 */
package sailing.ui.systemsetting;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.application.SingleFrameApplication;

import sailing.entity.CashAccount;
import sailing.listener.DATA;
import sailing.listener.DataListener;
import sailing.managers.CashAccountManager;
import sailing.ui.AbstractJPanel;
import sailing.ui.base.TopTitlePanel;

import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * @author kevin
 * 
 */
public class CashAccountMgtPanel extends AbstractJPanel implements DataListener {

	private JButton initialCashBtn;

	private static Font font = new Font("微软雅黑", Font.PLAIN, 20);

	private JPanel centerPanel;

	private CashAccount lastCA;

	private static final SimpleDateFormat onlyDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

	public CashAccountMgtPanel(SingleFrameApplication application) {
		super(application);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see sailing.ui.AbstractJPanel#initialPanel()
	 */
	@Override
	protected void initialPanel() {
		this.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 0));
		this.setLayout(new BorderLayout(10, 10));
		centerPanel = new JPanel();
		//this.add(this.getTopTitlePanel(), BorderLayout.NORTH);
		this.add(new TopTitlePanel("库存现金账户管理"), BorderLayout.NORTH);
		this.getCenterPanel(centerPanel);
		this.add(centerPanel, BorderLayout.CENTER);
		DATA.addDataListener(this);
	}

	private JPanel getCenterPanel(JPanel centerPanel) {
		CashAccountManager cam = this.getApplication().getContext().getIoc().getInstance(CashAccountManager.class);
		this.lastCA = cam.getCashAccount();

		centerPanel.removeAll();
		centerPanel.setLayout(new BorderLayout(10, 10));
		if (!isInitial()) {
			centerPanel.add(this.getCashMgtCenterPanel(), BorderLayout.CENTER);
		} else {
			centerPanel.add(this.getCashMgtInitCenterPanel(), BorderLayout.CENTER);
		}
		centerPanel.add(this.getCashMgtSidebarPanel(), BorderLayout.EAST);
		return centerPanel;
	}

	private Component getCashMgtInitCenterPanel() {
		JPanel root = new FormDebugPanel(true, true);
		root.setBorder(BorderFactory.createEmptyBorder(50, 20, 50, 20));
		root.setLayout(new FlowLayout(FlowLayout.CENTER));
		// Box box = Box.createVerticalBox();
		// JLabel label = new JLabel("还没有初始化现金账户");
		// label.setFont(font);
		// label.setForeground(Color.red);
		// box.add(label);
		//

		//JPanel root2 = new FormDebugPanel(true, true);
		JPanel root2 = new JPanel();
		FormLayout layout = new FormLayout("r:pref,3dlu,f:100dlu,3dlu,l:50dlu", "30dlu,10dlu,30dlu");
		root2.setLayout(layout);
		CellConstraints cc = new CellConstraints();
		// row 1
		JLabel label = new JLabel("库存现金：");
		label.setFont(font);
		root2.add(label, cc.xy(1, 1));

		label = new JLabel(String.valueOf(this.lastCA.getInitialBalance()));
		label.setFont(font);
		root2.add(label, cc.xy(3, 1));

		label = new JLabel("(单位：元)");
		label.setFont(font);
		root2.add(label, cc.xy(5, 1));

		// row 2
		label = new JLabel("库存现金启用时间：");
		label.setFont(font);
		root2.add(label, cc.xy(1, 3));

		Date date = new Date(this.lastCA.getOpenTime());
		String opentime = onlyDateFormat.format(date);

		label = new JLabel(opentime);
		label.setFont(font);
		root2.add(label, cc.xy(3, 3));

		root.add(root2);

		return root;
	}

	private boolean isInitial() {
		return lastCA != null;
	}

	private JPanel getCashMgtCenterPanel() {
		// JPanel root = new JPanel();
		JPanel root = new FormDebugPanel(true, true);
		root.setBorder(BorderFactory.createEmptyBorder(50, 20, 50, 20));
		root.setLayout(new FlowLayout(FlowLayout.CENTER));

		Box box = Box.createVerticalBox();
		JLabel label = new JLabel("还没有初始化现金账户");
		label.setFont(font);
		label.setForeground(Color.red);
		box.add(label);
		//
		// FormLayout layout=new FormLayout("c:pref","c:pref");
		// root.setLayout(layout);
		// CellConstraints cc=new CellConstraints();
		// root.add(new JLabel("还没有初始化现金账户"),cc.xy(1, 1));
		root.add(box);

		return root;
	}

	private JPanel getCashMgtSidebarPanel() {
		JPanel panel = new JPanel();
		initialCashBtn = new JButton();
		if(!this.isInitial()){
			initialCashBtn.setText("初始化库存现金");
		}else{
			initialCashBtn.setText("修正库存现金金额");
		}
		
		initialCashBtn.setMargin(new Insets(5, 30, 5, 30));
		initialCashBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// // String cashierName = JOptionPane.showInputDialog(CashierMgtPanel.this, "");
				//
				// Object cashierName = JOptionPane.showInputDialog(CashierMgtPanel.this, "请输入经手人的名称：\n\n", "经手人",
				// JOptionPane.PLAIN_MESSAGE, null, null, null);
				//
				// if (cashierName == null) {
				// // means cancel
				// return;
				// }
				// String cashierNameStr = (String) cashierName;
				// save(cashierNameStr);

				CashAccountSettingDialog casd = new CashAccountSettingDialog(getApplication().getMainFrame(),
						getApplication());
				casd.setVisible(true);

			}
		});

		JCheckBox showhideEditBtn = new JCheckBox();
		showhideEditBtn.setText("显示隐藏编辑按钮");

		showhideEditBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// JCheckBox cb = (JCheckBox) e.getSource();
				// logger.trace("checkbox is selected[{0}]", cb.isSelected());
				// showHideEditBtn(cb.isSelected());
			}
		});

		Box bv = Box.createVerticalBox();
		bv.add(initialCashBtn);
		bv.add(Box.createVerticalStrut(20));
		bv.add(showhideEditBtn);
		panel.add(bv);
		return panel;
	}

	private JPanel getTopTitlePanel() {
		JPanel panel = new JPanel();
		panel.setBorder(BorderFactory.createEtchedBorder());
		panel.setBackground(titleBgColor);
		panel.add(new JLabel("库存现金账户管理"));
		return panel;
	}

	@Override
	public void dataChanged() {
		this.getCenterPanel(centerPanel);
		centerPanel.validate();
	}

}
